

set more off

***Set working directory - end users will want to change this
global workdir "E:\SRTR\HCV\replication kit\"

set scheme s1mono 
set graphics on
graph set window fontface "Times New Roman"
graph set eps fontface Times

cd "$workdir\output\"

cap log close
log using "$workdir\logs\summary stats.log", text replace


************************************************************************************************************
****** Main Summary Stats Tables ***************************************************************************
************************************************************************************************************

use "$workdir\outputdata\estimation sample.dta", clear

drop if missing(can_listing_opo_id)

** First, look at % of known HCV cases among transplant recipients
gen rec_hcv_li = .
replace rec_hcv_li = 0 if rec_hcv_stat_li == "N"
replace rec_hcv_li = 1 if rec_hcv_stat_li == "P"

gen rec_hcv_ki = .
replace rec_hcv_ki = 0 if rec_hcv_stat_ki == "N"
replace rec_hcv_ki = 1 if rec_hcv_stat_ki == "P"

tabstat rec_hcv_li rec_hcv_ki if inrange(can_rem_year,2005,2019), by(can_rem_year) stat(mean n)
tabstat rec_hcv_li rec_hcv_ki if inrange(can_rem_year,2005,2013), stat(mean n)


** Now, look at summary stats among HCV, Non-HCV Liver Candidates, as well as KI candidates
drop if wl_org=="KI" & can_dgn_hep==1 // drops any KI candidates that were marked as HCV-related diagnosis in data prep file
replace can_dgn_hep = 1 if wl_org=="KI"
*drop if missing(can_dgn_hep) // this drops LI candidates with diagnosis codes that are neither classified as hcv or non-hcv

gen race = "White" if can_race==8
replace race = "NonWhite" if can_race!=8

gen age_group = "18to39" if inrange(can_age_at_listing,18,39)
replace age_group = "40to64" if inrange(can_age_at_listing,40,64)
replace age_group = "Over64" if inrange(can_age_at_listing,65,130)

cap drop opo_state
gen opo_state = substr(served_opo_cd,1,2)
gen censusregion = "South" if inlist(opo_state,"FL","PR","GA","AL","MS","LA","TX","OK","AR")
replace censusregion = "South" if inlist(opo_state,"TN","KY","WV","MD","DE","DC","VA","NC","SC")
replace censusregion = "Northeast" if inlist(opo_state,"ME","NH","VT","MA","RI","CT","NJ","NY","PA")
replace censusregion = "Midwest" if inlist(opo_state,"OH","MI","IN","IL","WI","MO","IA","MN")
replace censusregion = "Midwest" if inlist(opo_state,"ND","SD","NE","KS","MW")
replace censusregion = "West" if inlist(opo_state,"WA","OR","CA","AK","HI","AZ","NM","CO","UT")
replace censusregion = "West" if inlist(opo_state,"NV","ID","MT","WY")
encode censusregion, gen(censusregion1)

gen white = 0 if !missing(race)
replace white = 1 if race=="White"

gen private = 0 if !missing(ins_alt)
replace private = 1 if ins_alt=="Private"

gen medicare = 0 if !missing(ins_alt)
replace medicare = 1 if ins_alt=="Medicare"

cap drop medicaid // drops variable indicating whether person has medicaid as either primary or secondary
gen medicaid = 0 if !missing(ins_alt)
replace medicaid = 1 if ins_alt=="Medicaid"

gen age18to39 = 0 if !missing(age_group)
replace age18to39 = 1 if age_group=="18to39"

gen age40to64 = 0 if !missing(age_group)
replace age40to64 = 1 if age_group=="40to64"

gen ageOver64 = 0 if !missing(age_group)
replace ageOver64 = 1 if age_group=="Over64"

gen south = 0 if !missing(censusregion)
replace south = 1 if censusregion=="South"

gen northeast = 0 if !missing(censusregion)
replace northeast = 1 if censusregion=="Northeast"

gen midwest = 0 if !missing(censusregion)
replace midwest = 1 if censusregion=="Midwest"

gen west = 0 if !missing(censusregion)
replace west = 1 if censusregion=="West"

gen hcv_rate_li = 0 if wl_org=="LI"
replace hcv_rate_li = 1 if can_dgn_hep==1 & wl_org=="LI"

gen hcv_unknown = 0 if wl_org=="LI"
replace hcv_unknown = 1 if missing(can_dgn_hep) & wl_org=="LI"

gen daystotx = rec_tx_dt - can_listing_dt

****************************************************
**** Appendix Table 6 ******************************
****************************************************

** Livers
tabstat hcv_rate_li hcv_unknown initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & inrange(can_listing_year,2005,2019), col(stat) stat(mean sd)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & inrange(can_rem_year,2005,2019), col(stat) stat(mean sd)

tabstat hcv_rate_li hcv_unknown initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & inrange(can_listing_year,2005,2013), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & inrange(can_rem_year,2005,2013), col(stat) stat(mean)

tabstat hcv_rate_li hcv_unknown initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & inrange(can_listing_year,2014,2019), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & inrange(can_rem_year,2014,2019), col(stat) stat(mean)

** Kidneys
tabstat educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="KI" & inrange(can_listing_year,2005,2019), col(stat) stat(mean sd)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="KI" & inrange(can_rem_year,2005,2019), col(stat) stat(mean sd)

tabstat educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="KI" & & inrange(can_listing_year,2005,2013), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="KI" & inrange(can_rem_year,2005,2013), col(stat) stat(mean)

tabstat educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="KI" & inrange(can_listing_year,2014,2019), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="KI" & inrange(can_rem_year,2014,2019), col(stat) stat(mean)


****************************************************
**** Table 1 ***************************************
****************************************************

drop if missing(can_dgn_hep) // this drops LI candidates with diagnosis codes that are neither classified as hcv or non-hcv

** Livers, HCV vs. Non-HCV
tabstat initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_listing_year,2005,2019), col(stat) stat(mean sd)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & can_dgn_hep==1 & inrange(can_rem_year,2005,2019), col(stat) stat(mean sd)

tabstat initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_listing_year,2005,2013), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & can_dgn_hep==1 & inrange(can_rem_year,2005,2013), col(stat) stat(mean)

tabstat initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_listing_year,2014,2019), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & can_dgn_hep==1 & inrange(can_rem_year,2014,2019), col(stat) stat(mean)

** Non-HCV
tabstat initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_listing_year,2005,2019), col(stat) stat(mean sd)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & can_dgn_hep==0 & inrange(can_rem_year,2005,2019), col(stat) stat(mean sd)

tabstat initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_listing_year,2005,2013), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & can_dgn_hep==0 & inrange(can_rem_year,2005,2013), col(stat) stat(mean)

tabstat initial_meld educ_lteqhs white private medicare medicaid age18to39 age40to64 ageOver64 south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_listing_year,2014,2019), col(stat) stat(mean)
tabstat toosickdied improved ddtx ldtx daystotx if wl_org=="LI" & can_dgn_hep==0 & inrange(can_rem_year,2014,2019), col(stat) stat(mean)

* N of Listings
* HCV
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_listing_year,2005,2019), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_listing_year,2005,2013), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_listing_year,2014,2019), col(stat) stat(mean sd n)

* Non-HCV
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_listing_year,2005,2019), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_listing_year,2005,2013), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_listing_year,2014,2019), col(stat) stat(mean sd n)


* N of Removals
* HCV
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_rem_year,2005,2019), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_rem_year,2005,2013), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==1 & inrange(can_rem_year,2014,2019), col(stat) stat(mean sd n)

* Non-HCV
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_rem_year,2005,2019), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_rem_year,2005,2013), col(stat) stat(mean sd n)
tabstat initial_meld white south northeast midwest west if wl_org=="LI" & can_dgn_hep==0 & inrange(can_rem_year,2014,2019), col(stat) stat(mean sd n)



************************************************************************************************************
****** WL ADDS Graphs **************************************************************************************
************************************************************************************************************

use "$workdir\outputdata\estimation sample.dta", clear


*****************************************
*** Figure 2b ***************************
*****************************************

preserve
drop if wl_org=="KI"

gen can_dgn_unknown = 1 if can_dgn_hep!=1 & can_dgn_nothep!=1

collapse (sum) can_dgn_hep can_dgn_nothep can_dgn_unknown, by(can_listing_year)

gen can_dgn_total = can_dgn_hep + can_dgn_nothep + can_dgn_unknown
gen can_dgn_both = can_dgn_hep + can_dgn_nothep

drop if !inrange(can_listing_year,2005,2019)

twoway  conn can_dgn_hep can_listing_year || ///
		conn can_dgn_nothep can_listing_year, lpattern(dash) || ///
		conn can_dgn_both can_listing_year, lpattern(shortdash) || ///
		, ///
		legend(label(1 "HCV") label(2 "Non-HCV") label(3 "HCV + Non-HCV")) ylabel(0(2000)12000) ///
		title(Liver Waitlist Additions by HCV Status) ytitle(Number of Additions) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8) legend(rows(1))
graph export WL_LI_Adds_with_Total.eps, as(eps) replace

restore

******************************

*****************************************
*** Figure 4b ***************************
*****************************************

preserve

drop if wl_org=="KI" & can_dgn_hep==1 // drops any KI candidates that were marked as HCV-related diagnosis in data prep file
replace can_dgn_nothep = 1 if wl_org=="KI"

collapse (sum) can_dgn_hep can_dgn_nothep, by(can_listing_year wl_org)

drop if !inrange(can_listing_year,2005,2019)

gen lncandgnhep = ln(can_dgn_hep)
gen lncandgnnothep = ln(can_dgn_nothep)

gen baseline2012hep = lncandgnhep if can_listing_year==2012
gen baseline2012nothep = lncandgnnothep if can_listing_year==2012

bysort wl_org: egen baseline2012hep1 = max(baseline2012hep)
bysort wl_org: egen baseline2012nothep1 = max(baseline2012nothep)

gen lncandgnhep_mod = lncandgnhep - baseline2012hep1
gen lncandgnnothep_mod = lncandgnnothep - baseline2012nothep1

sort wl_org can_listing_year

twoway  conn lncandgnhep_mod can_listing_year || ///
		conn lncandgnnothep_mod can_listing_year if wl_org=="LI", lpattern(dash) || ///
		conn lncandgnnothep_mod can_listing_year if wl_org=="KI", lpattern(shortdash) || ///
		, ///
		legend(label(1 "Livers, HCV") label(2 "Livers, Non-HCV") label(3 "Kidneys")) ///
		title("Log Waitlist Additions by HCV Status and Organ," "Deviations from 2012 Baseline") ytitle(Log of Number of Additions) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
graph export Log_WL_LIandKI_Adds.eps, as(eps) replace

restore

******************************************


*****************************************
*** Figure 3b ***************************
*****************************************

***MELD

preserve
drop if wl_org=="KI"

*gen initial_meld = can_init_srtr_lab_meld - 6200

collapse (mean) initial_meld, by(can_dgn_hep can_listing_year)

drop if !inrange(can_listing_year,2005,2019)

	twoway  conn initial_meld can_listing_year if can_dgn_hep==1 || ///
			conn initial_meld can_listing_year if can_dgn_hep==0, lpattern(dash) || ///
			, ///
			legend(label(1 "HCV") label(2 "Non-HCV")) ///
			title(Average Initial MELD Score of WL Adds by HCV Status) ytitle(Average Initial MELD Score) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8) ylabel(15(2)27)
	graph export Avg_MELD_by_HCVdgn.eps, as(eps) replace

restore

************************************


*****************************************
*** Appendix Figure 6a ******************
*****************************************

*** Willingness to accept HCV liver over time

preserve
*drop if wl_org=="LI"
drop if wl_org=="KI" & can_dgn_hep==1 // drops any KI candidates that were marked as HCV-related diagnosis in data prep file
drop if can_dgn_hep==1 // drops HCV liver candidates too
drop if missing(can_dgn_hep) & wl_org=="LI"

*gen acpthcvdon = 0 if !missing(can_acpt_hcv_pos)
*replace acpthcvdon = 1 if can_acpt_hcv_pos=="Y"

collapse (mean) acpthcvdon, by(wl_org can_listing_year)

drop if !inrange(can_listing_year,2005,2019)

	twoway  conn acpthcvdon can_listing_year if wl_org=="KI" || ///
			conn acpthcvdon can_listing_year if wl_org=="LI" || ///
			, ///
			legend(label(1 "Kidney Registrants") label(2 "Non-HCV Liver Registrants")) ///
			title("Fraction of New WL Adds Willing to Accept HCV Donor") ytitle(Fraction Willing to Accept HCV Donor) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export Acpt_HCV_LIandKI.eps, as(eps) replace
	
	
restore

******************************


***********************************************************
****** Size, Composition, and WL Removals Graphs **********
***********************************************************

preserve
drop if wl_org=="KI"

*drop if missing(can_dgn_hep)

drop if !inrange(can_rem_year,2005,2019)

/*
gen last_meld = can_last_srtr_lab_meld - 6200

foreach x in toosickdied improved ddtx ldtx tx {
	gen last_meld_`x' = last_meld if `x'==1
}
*/

gen daystotx = rec_tx_dt - can_listing_dt
sum daystotx if wl_org=="LI" & can_rem_year<2013, detail
local medLIdtt = r(p50)
sum daystotx if wl_org=="KI" & can_rem_year<2013, detail
local medKIdtt = r(p50)

gen txfasterthanmedian = 0 
replace txfasterthanmedian = 1 if daystotx<=`medLIdtt' & wl_org=="LI"
replace txfasterthanmedian = 1 if daystotx<=`medKIdtt' & wl_org=="KI"

collapse (sum) toosickdied improved ddtx ldtx tx (mean) daystotx last_meld_* , by(wl_org can_dgn_hep can_rem_ym can_rem_year)

gen wl_ym = can_rem_ym
merge m:1 wl_ym wl_org using "$workdir/outputdata/WL composition.dta" // this brings in size and composition variables
drop if _merge!=3

format can_rem_ym %tm

gen numonwlboth = numonwlhcv + numonwlnonhcv

collapse (sum) toosickdied improved ddtx ldtx tx (mean) daystotx lab_meld lab_meld_hcv lab_meld_nonhcv last_meld_* numonwl numonwlhcv numonwlnonhcv numonwlboth, by(can_dgn_hep can_rem_year)

*****************************************
*** Figure 2a ***************************
*****************************************

** Number on WL by HCV

	twoway  conn numonwlhcv can_rem_year if can_dgn_hep==1 || ///
			conn numonwlnonhcv can_rem_year if can_dgn_hep==0, lpattern(dash) || ///
			conn numonwlboth can_rem_year if can_dgn_hep==0, lpattern(shortdash) || ///
			, ///
			legend(label(1 "HCV") label(2 "Non-HCV") label(3 "HCV + Non-HCV")) ///
			title(Number of Waitlist Registrants by HCV Status) ytitle(Number of WL Registrants) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8) ylabel(0(4000)16000) legend(rows(1))
	graph export NumberofCandidates_by_HCVdgn_with_Total.eps, as(eps) replace	
	
***********************************
	
foreach x in toosickdied improved tx ldtx {
	gen `x'_pct = `x'/numonwlhcv if can_dgn_hep==1	
	replace `x'_pct = `x'/numonwlnonhcv if can_dgn_hep==0
}	

foreach x in toosickdied improved ddtx ldtx tx {
	gen ln`x'=ln(1+`x')
}

gen lndaystotx = ln(1+daystotx)

foreach x in tx toosickdied improved {
	bysort can_rem_year: egen sum`x' = sum(`x') if inlist(can_dgn_hep,0,1)
	}

sort can_rem_year can_dgn_hep

*****************************************
*** Appendix Figure 7 *******************
*****************************************

	twoway  conn daystotx can_rem_year if can_dgn_hep==0 || ///
			, ///
			legend(label(1 "Non-HCV")) ///
			title(Days to TX among Non-HCV Recipients) ytitle(Days) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export DaysToTX_NonHCV.eps, as(eps) replace
	
***********************************


*****************************************
*** Appendix Figure 1a ******************
*****************************************

	twoway  conn toosickdied_pct can_rem_year if can_dgn_hep==1 || ///
			conn toosickdied_pct can_rem_year if can_dgn_hep==0, lpattern(dash) || ///
			, ///
			legend(label(1 "HCV") label(2 "Non-HCV")) ///
			title("Fraction of Registrants Removed b/c Too Sick or Died" "by HCV Status") ytitle(Fraction of Registrants Removed) xtitle(Year) xlabel(2005(1)2019, angle(45)) ylabel(0(.05)0.2) xline(2013.8)
	graph export TooSickDiedPct_by_HCVdgn.eps, as(eps) replace

**************************************
	
*****************************************
*** Appendix Figure 1b ******************
*****************************************

	twoway  conn improved_pct can_rem_year if can_dgn_hep==1 || ///
			conn improved_pct can_rem_year if can_dgn_hep==0, lpattern(dash) || ///
			, ///
			legend(label(1 "HCV") label(2 "Non-HCV")) ///
			title("Fraction of Registrants Removed b/c Improved Condition" "by HCV Status") ytitle(Fraction of Registrants Removed) xtitle(Year) xlabel(2005(1)2019, angle(45)) ylabel(0(.05)0.2) xline(2013.8)
	graph export ImprovedPct_by_HCVdgn.eps, as(eps) replace

**************************************


*****************************************
*** Figure 1 ****************************
*****************************************

	twoway  conn tx can_rem_year if can_dgn_hep==1 || ///
			conn tx can_rem_year if can_dgn_hep==0, lpattern(dash) || ///
			conn sumtx can_rem_year if can_dgn_hep==0, lpattern(shortdash) || ///
			, ///
			legend(label(1 "HCV") label(2 "Non-HCV") label(3 "HCV + Non-HCV")) ylabel(0(2000)8000) ///
			title(Liver Transplants by HCV Status) ytitle(Number of Transplants) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8) legend(rows(1))
	graph export Transplanted_by_HCVdgn_with_Total.eps, as(eps) replace
	
***************************************


*****************************************
*** Appendix Figure 2 *******************
*****************************************

	twoway  conn tx_pct can_rem_year if can_dgn_hep==1 || ///
			conn tx_pct can_rem_year if can_dgn_hep==0, lpattern(dash) || ///
			, ///
			legend(label(1 "HCV") label(2 "Non-HCV")) ///
			title("Fraction of Registrants Transplanted by HCV Status") ytitle(Fraction of Registrants Transplanted) xtitle(Year) xlabel(2005(1)2019, angle(45)) ylabel(0(.1)0.7) xline(2013.8)
	graph export TransplantedPct_by_HCVdgn.eps, as(eps) replace

*************************************	
	

*****************************************
*** Figure 3a ***************************
*****************************************
	
	twoway  conn last_meld_tx can_rem_year if can_dgn_hep==1 || ///
			conn last_meld_tx can_rem_year if can_dgn_hep==0, lpattern(dash) || ///
			, ///
			legend(label(1 "HCV") label(2 "Non-HCV")) ///
			title("Average Last MELD Score of Transplant Recipients" "by HCV Status") ytitle(Average Last MELD Score) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8) ylabel(15(2)27)
	graph export Last_MELD_Transplanted_by_HCVdgn.eps, as(eps) replace
	
*************************************

restore

**************************************************************************


preserve

foreach x in ddtx ldtx tx {
	gen `x'_hcv = .
	replace `x'_hcv = 0 if rec_hcv_stat_ki=="N" | rec_hcv_stat_li=="N"
	replace `x'_hcv = 1 if rec_hcv_stat_ki=="P" | rec_hcv_stat_li=="P" // TX recipients with HCV
}

drop if !inrange(can_rem_year,2005,2019)

collapse (sum) ddtx_hcv ldtx_hcv tx_hcv (mean) hcvtxshare=tx_hcv , by(wl_org can_rem_year)

foreach x in ddtx_hcv ldtx_hcv tx_hcv {
	gen ln`x'=ln(1+`x')
}

*****************************************
*** Appendix Figure 6c ******************
*****************************************

	twoway  conn tx_hcv can_rem_year if wl_org=="LI", ylabel(0(500)3000) || ///
			conn tx_hcv can_rem_year if wl_org=="KI", lpattern(dash) || ///
			, ///
			legend(label(1 "Livers") label(2 "Kidneys")) ///
			title("Number of HCV+ Transplant Recipients by Organ") ytitle(Number of TX Recipients) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export KI_LI_TX_Recip_HCV.eps, as(eps) replace
	
**********************************

*****************************************
*** Appendix Figure 6d ******************
*****************************************
	
	twoway  conn hcvtxshare can_rem_year if wl_org=="LI" || ///
			conn hcvtxshare can_rem_year if wl_org=="KI", lpattern(dash) || ///
			, ///
			legend(label(1 "Livers") label(2 "Kidneys")) ///
			title("Share of HCV+ Transplant Recipients by Organ") ytitle(Fraction of TX Recipients) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export Share_KI_LI_TX_Recip_HCV.eps, as(eps) replace

**********************************

restore


**************************************************************************

preserve

drop if can_dgn_hep==1 & wl_org=="KI"
replace can_dgn_hep = 0 if wl_org=="KI"

drop if missing(can_dgn_hep)

drop if !inrange(can_rem_year,2005,2019)

gen ldtx_hcvdon = ldtx if hcv_donor==1
gen ldtx_nonhcvdon = ldtx if non_hcv_donor==1

collapse (sum) toosickdied improved tx ldtx_hcvdon ldtx_nonhcvdon , by(wl_org can_dgn_hep can_rem_ym can_rem_year)

gen wl_ym = can_rem_ym
merge m:1 wl_ym wl_org using "$workdir/outputdata/WL composition.dta" // this brings in size and composition variables
drop if _merge!=3

format can_rem_ym %tm

collapse (sum) toosickdied improved tx ldtx_hcvdon ldtx_nonhcvdon (mean) numonwl numonwlhcv numonwlnonhcv, by(wl_org can_dgn_hep can_rem_year)

foreach x in toosickdied improved tx ldtx_hcvdon ldtx_nonhcvdon numonwl numonwlhcv numonwlnonhcv {
	gen ln`x'=ln(1+`x')
	gen ln`x'2012 = ln`x' if can_rem_year==2012
	bysort wl_org can_dgn_hep: egen ln`x'2012_1 = max(ln`x'2012)
	gen ln`x'_mod = ln`x' - ln`x'2012_1
}

foreach x in toosickdied improved tx {
	gen `x'_pct = `x'/numonwlhcv if can_dgn_hep==1 & wl_org=="LI"
	replace `x'_pct = `x'/numonwlnonhcv if can_dgn_hep==0 & wl_org=="LI"
	replace `x'_pct = `x'/numonwl if can_dgn_hep==0 & wl_org=="KI"
	gen `x'_pct_2012 = `x'/numonwlhcv if can_dgn_hep==1	& can_rem_year==2012 & wl_org=="LI"
	replace `x'_pct_2012 = `x'/numonwlnonhcv if can_dgn_hep==0 & can_rem_year==2012 & wl_org=="LI"
	replace `x'_pct_2012 = `x'/numonwl if can_dgn_hep==0 & can_rem_year==2012 & wl_org=="KI"
	bysort wl_org can_dgn_hep: egen `x'_pct_2012_1 = max(`x'_pct_2012)
	gen `x'_pct_mod = `x'_pct - `x'_pct_2012_1
}	

sort wl_org can_rem_year

*****************************************
*** Figure 4a ***************************
*****************************************

	twoway  conn lntx_mod can_rem_year if can_dgn_hep==1 || ///
			conn lntx_mod can_rem_year if can_dgn_hep==0 & wl_org=="LI", lpattern(dash) || ///
			conn lntx_mod can_rem_year if can_dgn_hep==0 & wl_org=="KI", lpattern(shortdash) || ///
			, ///
			legend(label(1 "Livers, HCV") label(2 "Livers, Non-HCV") label(3 "Kidneys")) ///
			title("Log Transplants by HCV Status and Organ," "Deviations from 2012 Baseline") ytitle(Log Number of Transplants) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export Log_LIandKI_Transplanted_by_HCVdgn.eps, as(eps) replace

*************************************
	

*****************************************
*** Appendix Figure 6b ******************
*****************************************

	twoway  conn ldtx_hcvdon can_rem_year if wl_org=="KI", yaxis(1) ylabel(0(10)70, axis(1)) || ///
			conn ldtx_nonhcvdon can_rem_year if wl_org=="KI", lpattern(dash) yaxis(2) ylabel(0(1000)7000, axis(2)) || ///
			, ///
			legend(label(1 "HCV+ Donors") label(2 "HCV- Donors")) ///
			title("Number of Living Donor Kidney Transplants" "by Donor HCV Antibody Status") ytitle(Number of HCV+ Donor TXs, axis(1)) ytitle(Number of HCV- Donor TXs, axis(2)) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export KI_LDTX_by_Donor_HCV.eps, as(eps) replace	

**************************************
	
restore



************************************************************************************************************
****** Supply of Donors Graphs *****************************************************************************
************************************************************************************************************


preserve

use "$workdir\inputdata\donor_disposition.dta", clear

gen don_recov_year = year(don_recov_dt)

drop if !inrange(don_recov_year,2005,2019)

merge m:1 donor_id using "$workdir\inputdata\donor_deceased.dta", keepusing(don_anti_hcv)
drop if _merge==2

*don_anti_hcv tests whether you were ever infected with HCV
gen hcv_donor = .
replace hcv_donor = 0 if don_anti_hcv=="N"
replace hcv_donor = 1 if don_anti_hcv=="P"

gen non_hcv_donor = .
replace non_hcv_donor = 0 if don_anti_hcv=="P"
replace non_hcv_donor = 1 if don_anti_hcv=="N"

keep if inlist(don_org,"EKI","LI","LIS1","LIS2","LKI","RKI")

gen recovfortx_hcv = 0 if hcv_donor==1
replace recovfortx_hcv = 1 if inlist(don_disposition,5,6) & hcv_donor==1
replace recovfortx_hcv = 0.5 if inlist(don_org,"LIS1","LIS2") & hcv_donor==1
replace recovfortx_hcv = 2 if inlist(don_org,"EKI") & hcv_donor==1

gen recovfortx_nonhcv = 0 if hcv_donor==1
replace recovfortx_nonhcv = 1 if inlist(don_disposition,5,6) & hcv_donor==0
replace recovfortx_nonhcv = 0.5 if inlist(don_org,"LIS1","LIS2") & hcv_donor==0
replace recovfortx_nonhcv = 2 if inlist(don_org,"EKI") & hcv_donor==0

gen organ = "LI" if inlist(don_org,"LIS1","LIS2","LI")
replace organ = "KI" if inlist(don_org,"RKI","LKI","EKI")

collapse (sum) recovfortx_hcv recovfortx_nonhcv, by(don_recov_year organ)

*****************************************
*** Figure 6a ***************************
*****************************************

	twoway  conn recovfortx_hcv don_recov_year if organ=="LI", yaxis(1) ylabel(0(200)1000, axis(1))|| ///
			conn recovfortx_hcv don_recov_year if organ=="KI", lpattern(dash) yaxis(2) ylabel(0(500)2500, axis(2)) || ///
			, ///
			legend(label(1 "Livers") label(2 "Kidneys")) ///
			title("Number of Deceased Donor Organs Recovered for TX" "from HCV Donors") ytitle(Number of Livers, axis(1)) ytitle(Number of Kidneys, axis(2)) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export Organs_HCV_Donors.eps, as(eps) replace

*********************************

*****************************************
*** Figure 6b ***************************
*****************************************

	twoway  conn recovfortx_nonhcv don_recov_year if organ=="LI", yaxis(1) ylabel(6000(1000)10000, axis(1))  || ///
			conn recovfortx_nonhcv don_recov_year if organ=="KI", lpattern(dash) yaxis(2) ylabel(12000(2000)20000, axis(2))  || ///
			, ///
			legend(label(1 "Livers") label(2 "Kidneys")) ///
			title("Number of Deceased Donor Organs Recovered for TX" "from Non-HCV Donors") ytitle(Number of Livers, axis(1)) ytitle(Number of Kidneys, axis(2)) xtitle(Year) xlabel(2005(1)2019, angle(45)) xline(2013.8)
	graph export Organs_NonHCV_Donors.eps, as(eps) replace


*********************************

restore

log close